package com.miao.teacher.ui;

import java.io.File;
import com.espeaker.sdk.utils.Base64Util;
import com.miao.teacher.app.TApp;
import com.miao.teacher.models.Recorder;
import com.miao.teacher.presenters.OnReviseListener;
import com.miao.teacher.utils.FileUtil;
import com.miao.teacher.utils.TMediaPlayer;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SummaryFragment  extends BaseFragment{
	
	private OnReviseListener mListener = null;
	
	private Recorder mCommentRecord = null;
	private Recorder mAdviseRecord = null;
	private String tempRecordFile = FileUtil.SDPATH + "/tofelmiao/sound/temp/temp_comment_{0}.amr";
	private String tempAdviseRecordFile = FileUtil.SDPATH + "/tofelmiao/sound/temp/temp_advise_{0}.amr";
	private TMediaPlayer mVoicePlayer = null;
	
	private TextView tvTimeComment;
	private TextView tvTimeAdvise;
	
	private Button btnAudioComment;
	private Button btnAudioAdvise;
	
	private LinearLayout llyAudio;
	private LinearLayout llyAudioAdvise;
	
	private ImageView animComment;
	private ImageView animAdvise;
	
	private AnimationDrawable playAnim = null;
	
	private boolean IsCommentRecord=false;
	private boolean IsAdviseRecord=false;
	
	/*
	 * 创建一个实例
	 */
	public static SummaryFragment newInstance() {
		SummaryFragment fragment = new SummaryFragment();
		return fragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.layout_my_summary, null);
		initView(view);
		return view;
	}
	private void initView(View view)
	{
		mVoicePlayer = new TMediaPlayer();
		tvTimeComment=(TextView)view.findViewById(R.id.tvTime);
		tvTimeAdvise=(TextView)view.findViewById(R.id.tvTime2);
		btnAudioComment=(Button)view.findViewById(R.id.btnAudioComent);
		btnAudioAdvise=(Button)view.findViewById(R.id.btnAudioAdvise);
		llyAudio=(LinearLayout)view.findViewById(R.id.lly_audio);
		llyAudioAdvise=(LinearLayout)view.findViewById(R.id.lly_audio_advise);
		animComment=(ImageView)view.findViewById(R.id.imgAudio);
		animAdvise=(ImageView)view.findViewById(R.id.imgAudio_adivse);
		
		tvTimeComment.setVisibility(View.INVISIBLE);
		tvTimeAdvise.setVisibility(View.INVISIBLE);
		llyAudio.setVisibility(View.INVISIBLE);
		llyAudioAdvise.setVisibility(View.INVISIBLE);
		animComment.setVisibility(View.INVISIBLE);
		animAdvise.setVisibility(View.INVISIBLE);
		
		//btnAudioComment.setOnTouchListener(new CommentOnTouchListener());
		//btnAudioAdvise.setOnTouchListener(new AdviseOnTouchListener());
		
		llyAudio.setOnClickListener(new CommentPlay());
		llyAudioAdvise.setOnClickListener(new AdvisePlay());
		
		tempRecordFile=tempRecordFile.replace("{0}", mListener.getReviseJson().xiaofei_id+"");
		tempAdviseRecordFile=tempAdviseRecordFile.replace("{0}", mListener.getReviseJson().xiaofei_id+"");
		loadData();
		
		btnAudioComment.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(IsCommentRecord)
				{
					IsCommentRecord=false;
					onCommentKeyUp();
				}else
				{
					if(IsAdviseRecord)
					{
						btnAudioAdvise.performClick();
					}
					IsCommentRecord=true;
					onCommentKeyDown();
				}
			}
			
		});
		btnAudioAdvise.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(IsAdviseRecord)
				{
					IsAdviseRecord=false;
					onAdviseKeyUp();
				}else
				{
					if(IsCommentRecord)
					{
						btnAudioComment.performClick();
					}
					IsAdviseRecord=true;
					onAdviseKeyDown();;
				}
			}
			
		});
	}
	/*
	 * 一句话评价 按住录音
	 */
	class CommentOnTouchListener implements OnTouchListener{
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				onCommentKeyDown();
				break;
			case MotionEvent.ACTION_UP:
				onCommentKeyUp();
				break;
			case MotionEvent.ACTION_CANCEL:
				onCommentKeyUp();
				break;
			}
			return false;
		}
	}
	/*
	 * 学习建议 按住录音
	 */
	class AdviseOnTouchListener implements OnTouchListener{
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				onAdviseKeyDown();
				break;
			case MotionEvent.ACTION_UP:
				onAdviseKeyUp();
				break;
			case MotionEvent.ACTION_CANCEL:
				onAdviseKeyUp();
				break;
			}
			return false;
		}
	}
	/*
	 * 播放评论录音
	 */
	class CommentPlay implements OnClickListener
	{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			playRecord(tempRecordFile);
			animComment.setBackgroundResource(R.drawable.anim_revise_playing);
			if (playAnim != null) {
				playAnim.stop();
			}
			playAnim = (AnimationDrawable) animComment.getBackground();
			playAnim.start();
		}
		
	}
	/*
	 * 播放建议录音
	 */
	class AdvisePlay implements OnClickListener
	{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			playRecord(tempAdviseRecordFile);
			animAdvise.setBackgroundResource(R.drawable.anim_revise_playing);
			if (playAnim != null) {
				playAnim.stop();
			}
			playAnim = (AnimationDrawable) animAdvise.getBackground();
			playAnim.start();
		}
		
	}
	/*
	 * 按住评论按钮
	 */
	private void onCommentKeyDown()
	{	
		llyAudio.setVisibility(View.VISIBLE);
		animComment.setVisibility(View.INVISIBLE);
		tvTimeComment.setVisibility(View.INVISIBLE);
		DoStartCommentRecord();
	}
	/*
	 * 按住一句话建议按钮
	 */
	private void onAdviseKeyDown()
	{
		llyAudioAdvise.setVisibility(View.VISIBLE);
		animAdvise.setVisibility(View.INVISIBLE);
		tvTimeAdvise.setVisibility(View.INVISIBLE);
		DoStartAdviseRecord();
	}
	/*
	 * 松开评论按钮
	 */
	private void onCommentKeyUp()
	{
		animComment.setVisibility(View.VISIBLE);
		tvTimeComment.setVisibility(View.VISIBLE);
		DoStopCommentRecord();
		int length=mVoicePlayer.getLength(tempRecordFile);
		tvTimeComment.setText(length/1000+"'");
		mListener.getReviseJson().commentLength=length;
		mListener.getReviseJson().commentPath=tempRecordFile;
		
		File tempFile = new File(tempRecordFile);
		byte[] mOverallBytes = com.miao.teacher.utils.ConvertUtils
		.getBytesFromFile(tempFile);
		mListener.getReviseJson().overall_revise = Base64Util.encodeStr(mOverallBytes);
	}
	/*
	 * 松开一句话建议按钮
	 */
	private void onAdviseKeyUp()
	{
		animAdvise.setVisibility(View.VISIBLE);
		tvTimeAdvise.setVisibility(View.VISIBLE);
		DoStopAdviseRecord();
		int length=mVoicePlayer.getLength(tempAdviseRecordFile);
		tvTimeAdvise.setText(length/1000+"'");
		mListener.getReviseJson().adviseLength=length;
		mListener.getReviseJson().advisePath=tempAdviseRecordFile;
		
		File tempFile = new File(tempAdviseRecordFile);
		byte[] mBytes = com.miao.teacher.utils.ConvertUtils
		.getBytesFromFile(tempFile);
		mListener.getReviseJson().suggestion_revise = Base64Util.encodeStr(mBytes);
	}
	/*
	 * 
	 */
	private void loadData()
	{
		if(mListener.getReviseJson().commentLength>0)
		{
			llyAudio.setVisibility(View.VISIBLE);
			animComment.setVisibility(View.VISIBLE);
			tvTimeComment.setVisibility(View.VISIBLE);
			int length=mVoicePlayer.getLength(tempRecordFile);
			tvTimeComment.setText(length/1000+"'");
		}
		if(mListener.getReviseJson().adviseLength>0)
		{
			llyAudioAdvise.setVisibility(View.VISIBLE);
			animAdvise.setVisibility(View.VISIBLE);
			tvTimeAdvise.setVisibility(View.VISIBLE);
			int length=mVoicePlayer.getLength(tempAdviseRecordFile);
			tvTimeAdvise.setText(length/1000+"'");
		}
	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try {
			mListener = (OnReviseListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ "must implement OnReviseListener");
		}
	}
	/*
	 * 播放录音
	 */
	public void playRecord(String url)
	{
		mVoicePlayer.ASycPlay(url, mHandler);
	}
	/*
	 * 停止播放录音
	 */
	public void stopPlayRecord()
	{
		if(mVoicePlayer!=null)
		{
			mVoicePlayer.DoStop();
		}
	}
	/**************************** 开始评论录音 ***********************************************/
	public void DoStartCommentRecord() {
		File tempFile = new File(tempRecordFile);
		if (tempFile.exists()) {
			tempFile.delete();
		}
		File APath = new File(FileUtil.SDPATH + "tofelmiao/sound/temp/");
		if (!APath.exists()) {
			FileUtil.DoCreateDir("tofelmiao/sound/temp/");
		}
		mCommentRecord = Recorder.create(tempFile);
		mCommentRecord.start();
	}

	/********************************* 停止评论录音 ***************************************/
	public void DoStopCommentRecord() {
		if (mCommentRecord != null) {
			mCommentRecord.stop();
		}
	}
	/**************************** 开始建议录音 ***********************************************/
	public void DoStartAdviseRecord() {
		File tempFile = new File(tempAdviseRecordFile);
		if (tempFile.exists()) {
			tempFile.delete();
		}
		File APath = new File(FileUtil.SDPATH + "tofelmiao/sound/temp/");
		if (!APath.exists()) {
			FileUtil.DoCreateDir("tofelmiao/sound/temp/");
		}
		mAdviseRecord = Recorder.create(tempFile);
		mAdviseRecord.start();
	}

	/********************************* 停止建议录音 ***************************************/
	public void DoStopAdviseRecord() {
		if (mAdviseRecord != null) {
			mAdviseRecord.stop();
		}
	}
	@Override
	protected void RecMsgFromHandler(Message msg) {
		// TODO Auto-generated method stub
		super.RecMsgFromHandler(msg);
		switch (msg.what) {
		case TApp.GOLDACTIVITY_PLAYFINISH:
			if (playAnim != null) {
				playAnim.stop();
			}
			break;
		}
	}

}
